﻿using Newtonsoft.Json.Converters;
using Newtonsoft.Json;

namespace DFGK_WMS.Common
{
    public static partial class Extend
    {
        /// <summary>
        /// 转换为指定类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="result"></param>
        /// <returns>是否成功</returns>
        public static bool ToConvert<T>(this object obj, out T result)
        {
            result = default(T);
            try
            {
                result = (T)Convert.ChangeType(obj, typeof(T));
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static string ToJson(this object obj)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            return JsonConvert.SerializeObject(obj, timeConverter);
        }
    }
}
